<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="手机号">
                <a-input v-model="queryParam.phone" placeholder=""/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => {
                  this.queryParam = {}
                  $refs.table.refresh(true)
                }">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <s-table
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :data="loadData"
      >
        <span slot="avatar" slot-scope="text, record">
          <a-avatar :src="record.avatar"/>
        </span>
        <span slot="action" slot-scope="text, record">
          <template v-if="record.is_staff !== '1'">
            <a @click="handleEdit(record)" v-action:check>设为员工</a>
          </template>
        </span>
      </s-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import moment from 'moment'
import {STable, Ellipsis} from '@/components'
import {getList, edit} from '@/api/user'

const columns = [
  {
    title: '头像',
    dataIndex: 'avatar',
    scopedSlots: {customRender: 'avatar'}
  },
  {
    title: '姓名',
    align: 'center',
    dataIndex: 'nickName',
  },
  {
    title: '性别',
    align: 'center',
    dataIndex: 'sex',
    customRender: e => {
      return e === "0" ? '未知' : e === "1" ? '男' : '女'
    }
  },
  {
    title: '联系电话',
    align: 'center',
    dataIndex: 'phone',
  },
  {
    title: '注册时间',
    align: 'center',
    dataIndex: 'created_at',
    customRender: e => {
      return moment(e).format('YYYY-MM-DD HH:mm:ss')
    }
  },
  {
    title: '操作',
    align: 'center',
    dataIndex: 'action',
    width: '150px',
    scopedSlots: { customRender: 'action' }
  },
]

export default {
  name: 'application',
  components: {
    STable,
    Ellipsis,
  },
  data() {
    this.columns = columns
    return {
      // create model
      visible: false,
      confirmLoading: false,
      mdl: null,
      // 查询参数
      queryParam: {
        phone: ""
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        const requestParameters = Object.assign({
          openid: '1'
        }, parameter, this.queryParam)
        return getList(requestParameters)
          .then(res => {
            return res.data
          })
      },
      handleEdit(e) {
        console.log(e);
        edit({
          is_staff: "1",
          username: e.username,
        }, e.id).then(ret => {
          this.confirmLoading = false
          // 刷新表格
          this.$refs.table.refresh()
          this.$message.info('设置成功')
        })
      },
      selectedRowKeys: [],
      selectedRows: []
    }
  },
}
</script>
